package com.crm.service.impl.internalTrainingManager;

import com.alibaba.fastjson.JSONObject;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.internalTrainingManager.CustomerUserMapper;
import com.crm.model.entity.internalTrainingManager.CustomerUser;
import com.crm.model.entity.internalTrainingManager.CustomerUserExample;
import com.crm.model.vo.csd.customerManager.CustomerDetailVO;
import com.crm.service.csd.customerManager.CustomerDbService;
import com.crm.service.internalTrainingManager.CustomerUserDbService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 内训部领取客户
 * @Author renshaorong
 * @Date 2021/9/6
 */
@Service
public class CustomerUserDbServiceImpl implements CustomerUserDbService {

    @Autowired
    CustomerUserMapper customerUserMapper;
    @Autowired
    CustomerDbService customerDbService;

    @Override
    public Boolean insertSelective(CustomerUser customerUser){
        return customerUserMapper.insertSelective(customerUser) > 0;
    }

    @Override
    public List<CustomerUser> selectByCustomerAndOwner(String customerId, String ownerId){
        CustomerUserExample customerUserExample = new CustomerUserExample();
        CustomerUserExample.Criteria criteria = customerUserExample.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        if(StringUtils.isNotEmpty(ownerId)){
            criteria.andOwnerIdEqualTo(ownerId);
        }
        criteria.andDeleteStatusEqualTo((short)0);
        return customerUserMapper.selectByExample(customerUserExample);
    }

    @Override
    public Integer selectCountByOwner(String ownerId){
        CustomerUserExample customerUserExample = new CustomerUserExample();
        CustomerUserExample.Criteria criteria = customerUserExample.createCriteria();
        criteria.andOwnerIdEqualTo(ownerId);
        criteria.andDeleteStatusEqualTo((short)0);
        return customerUserMapper.selectByExample(customerUserExample).size();
    }

    @Override
    public boolean updateByPrimaryKeySelective(CustomerUser customerUser) {
        return customerUserMapper.updateByPrimaryKeySelective(customerUser) > 0;
    }

    @Override
    public List<JSONObject> selectCustomerByOwnerId(String ownerId) {
        List<JSONObject> result = new ArrayList<JSONObject>();
        CustomerUserExample customerUserExample = new CustomerUserExample();
        CustomerUserExample.Criteria criteria = customerUserExample.createCriteria();
        criteria.andOwnerIdEqualTo(ownerId);
        criteria.andDeleteStatusEqualTo((short) StatusCodeEnum.NORMAL_CODE.getState());
        List<CustomerUser> customerUserList = customerUserMapper.selectByExample(customerUserExample);
        customerUserList.stream().forEach(customerUser -> {
            JSONObject jsonObject = new JSONObject();
            //客户id
            jsonObject.put("customerId",customerUser.getCustomerId());
            //客户名
            CustomerDetailVO customerDetailVO = customerDbService.selectCustomerDetailVO(customerUser.getCustomerId());
            if(customerDetailVO!=null){
                jsonObject.put("customerName",customerDetailVO.getCustomerName());
            }
            result.add(jsonObject);
        });
        return result;
    }
}